#pragma once
#include <unordered_map>
#include "./enum.h"
#include <list>
#include <filesystem>
#include <vector>
#include <utility>
#include <sys/uio.h>
#include <cstring>
namespace Http
{

	class Request
	{
	public:
		Method method;
		Status status;
		bool keep_alive;
		//bool chunked;
		std::string protocol;

		std::string url;
		//int content_len;
		//int recieved_len;
		std::unordered_map<std::string, std::string> query;
		std::unordered_map<std::string, std::string> head;

		std::string content;
		int parse_request(char *beg, int size);
		struct iovec make_reply_line();
	};
	class Reply
	{
	public:
		std::unordered_map<std::string, std::string> head;
		Status status;
		std::vector<struct iovec> content_buf;
		struct iovec head_buf;
		struct iovec startline_buf;
		void set_head(std::string, std::string);
		void make_default_head();
		struct iovec make_head_buf();
		struct iovec make_buf(std::string &&s)
		{
			struct iovec v;
			v.iov_base = malloc(s.size());
			v.iov_len = s.size();
			memcpy(v.iov_base, s.data(), v.iov_len);
			return v;
		}
		int content_length(){
			int size=0;
			for (auto &i : content_buf)
			{
				size+=i.iov_len;
			}
			return size;
		}
		Reply(){
			head_buf.iov_base=nullptr;
			startline_buf.iov_base=nullptr;
		}
		~Reply()
		{
			for (auto &i : content_buf)
			{
				if (i.iov_base != nullptr)
					free(i.iov_base);
			}
			if(head_buf.iov_base)
			free(head_buf.iov_base);
			if(startline_buf.iov_base)
			free(startline_buf.iov_base);
		}
	};

}
